package com.hs.jhzd.controller;

import com.hs.jhzd.entity.MissingSwipeRecord;
import com.hs.jhzd.repository.MissingSwipeRecordRepository;
import com.hs.jhzd.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/missingSwipe")
public class MissingSwipeRecordController {

  @Autowired
  private MissingSwipeRecordRepository repository;
  @Value("${jsbh}")
  private String jsbh;
  @RequestMapping("/save")
  @ResponseBody
  public boolean add(MissingSwipeRecord record) {
    record.setJsbh(jsbh);
    record.setJSMC(jsbh);
    repository.save(record);
    return true;
  }

  @RequestMapping("/findByJsh")
  @ResponseBody
  public List<MissingSwipeRecord> findByJsh(@RequestParam(required = true) String jsbh,
      @RequestParam(required = true) String jsh,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
      @RequestParam(required = true) String swipeType) {
    if (startTime == null) {
      startTime = DateUtils.getStartOfDay(new Date());
    }
    if (endTime == null) {
      endTime = new Date();
    }

    if (StringUtils.isBlank(swipeType)) {
      return repository.findByJsh(jsbh, jsh, startTime, endTime);
    } else {
      return repository.findByJshAndType(jsbh, jsh, startTime, endTime, swipeType);
    }
  }

  @RequestMapping("/findByJsbh")
  @ResponseBody
  public List<MissingSwipeRecord> findByJsbh(@RequestParam(required = true) String jsbh,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
      @RequestParam(required = true) String swipeType) {
    if (startTime == null) {
      startTime = DateUtils.getStartOfDay(new Date());
    }
    if (endTime == null) {
      endTime = new Date();
    }
    return repository.findByJsbh(jsbh, startTime, endTime, swipeType);
  }

}
